package com.nlp.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class SendMessage {
	private final static Logger LOGGER = Logger.getLogger(SendMessage.class);

	public static String sendByGet(String url, String parm) throws Exception {
		String query = url + "?param=" + URLEncoder.encode(parm, "UTF-8");
		GetMethod method = new GetMethod(query);
		LOGGER.debug(query);
		method.addRequestHeader("Content-type", "text/html");
		HttpClientManager.getHttpClient().executeMethod(method);
		// 捕获内存缓冲区的数据，转换成字节数组
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		// 获取请求响应的应答数据
		InputStream in = method.getResponseBodyAsStream();
		byte buf[] = new byte[64];
		int read = 0;
		while ((read = in.read(buf)) > 0) {// 从应答输入流中读取内容并写入out对象
			out.write(buf, 0, read);
		}
		if (in != null) { // 最后、关闭流
			in.close();
		}
		byte b[] = out.toByteArray();// 将out对象中存储的服务器应答的数据转为字节数组
		return new String(b, "utf-8");// 将字节数组扎转为json
	}

	public static String translateByPost(String urlStr, String param) {
		if (StringUtils.isBlank(urlStr) || StringUtils.isBlank(param))
			return "";
		String responseContent = "";
		try {
			PostMethod method = new PostMethod(urlStr);
			Header header = new Header();
			header.setName("Content-Type");
			header.setValue("application/json");
			RequestEntity entity = new StringRequestEntity(param, "application/json", "UTF-8");
			method.setRequestEntity(entity);
			method.addRequestHeader(header);
			int rst = HttpClientManager.getHttpClient().executeMethod(method);
			if (rst == HttpStatus.SC_OK) {
				InputStream in = method.getResponseBodyAsStream();
				BufferedReader rd = new BufferedReader(new InputStreamReader(in, "utf-8"));
				String tempLine = rd.readLine();
				StringBuffer tempStr = new StringBuffer();
				String crlf = System.getProperty("line.separator");
				while (tempLine != null) {
					tempStr.append(tempLine);
					tempStr.append(crlf);
					tempLine = rd.readLine();
				}
				responseContent = tempStr.toString();
				rd.close();
				in.close();
			}
		} catch (IOException e) {
		}
		return responseContent;
	}

	public static void main(String[] args) {
		String query = "太阳大小";
		String parm = "{\"version\":\"2.0\",\"service_id\":\"S14914\",\"log_id\":\"123\",\"session_id\":\"\",\"interactions\":[],\"request\":{\"user_id\":\"123456\",\"query\":\""+query+"\"}}";
		String url = "https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=24.d944b13baed4f88d92b5f0ffa114f0d6.2592000.1554952732.282335-15729350";
		url = "https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=24.d944b13baed4f88d92b5f0ffa114f0d6.2592000.1554952732.282335-15729350";
		String post = translateByPost(url, parm);
		System.out.println(post);
		
		
	}
}
